package com.qst.system.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qst.common.annotation.Log;
import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.enums.BusinessType;
import com.qst.system.domain.ScoreLevel;
import com.qst.system.domain.vo.ScoreLevelVo;
import com.qst.system.service.IScoreLevelService;
import com.qst.common.core.page.TableDataInfo;
import com.qst.common.utils.poi.ExcelUtil;

/**
 * 用户等级Controller
 * 
 * @author linzhe
 * @date 2025-08-25
 */
@RestController
@RequestMapping("/system/scorelevel")
public class ScoreLevelController extends BaseController
{
    @Autowired
    private IScoreLevelService scoreLevelService;

    /**
     * 查询用户等级列表
     */
    @PreAuthorize("@ss.hasPermi('system:scorelevel:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScoreLevel scoreLevel)
    {
        startPage();
        List<ScoreLevelVo> list = scoreLevelService.selectScoreLevelVoList(scoreLevel);
        return getDataTable(list);
    }

    /**
     * 导出用户等级列表
     */
    @PreAuthorize("@ss.hasPermi('system:scorelevel:export')")
    @Log(title = "用户等级", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ScoreLevel scoreLevel)
    {
        List<ScoreLevelVo> list = scoreLevelService.selectScoreLevelVoList(scoreLevel);
        ExcelUtil<ScoreLevelVo> util = new ExcelUtil<ScoreLevelVo>(ScoreLevelVo.class);
        return util.exportExcel(list, "用户等级数据");
    }

    /**
     * 获取用户等级详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:scorelevel:query')")
    @GetMapping(value = "/{scoreId}")
    public AjaxResult getInfo(@PathVariable("scoreId") Long scoreId)
    {
        return AjaxResult.success(scoreLevelService.selectScoreLevelById(scoreId));
    }

    /**
     * 新增用户等级
     */
    @PreAuthorize("@ss.hasPermi('system:scorelevel:add')")
    @Log(title = "用户等级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScoreLevel scoreLevel)
    {
        return toAjax(scoreLevelService.insertScoreLevel(scoreLevel));
    }

    /**
     * 修改用户等级
     */
    @PreAuthorize("@ss.hasPermi('system:scorelevel:edit')")
    @Log(title = "用户等级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScoreLevel scoreLevel)
    {
        return toAjax(scoreLevelService.updateScoreLevel(scoreLevel));
    }

    /**
     * 删除用户等级
     */
    @PreAuthorize("@ss.hasPermi('system:scorelevel:remove')")
    @Log(title = "用户等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scoreIds}")
    public AjaxResult remove(@PathVariable Long[] scoreIds)
    {
        return toAjax(scoreLevelService.deleteScoreLevelByIds(scoreIds));
    }
    
    /**
     * 查询公司名称列表
     */
    @PreAuthorize("@ss.hasPermi('system:scorelevel:list')")
    @GetMapping("/comName")
    public AjaxResult companyNameList() {
        List<String> list = scoreLevelService.selectCompanyNameList();
        // 构造与前端期望一致的格式
        List<Map<String, String>> result = new ArrayList<>();
        for (String companyName : list) {
            Map<String, String> map = new HashMap<>();
            map.put("comId", companyName);
            map.put("comName", companyName);
            result.add(map);
        }
        return AjaxResult.success(result);
    }
}